package com.wudl.govern.async;

import cn.hutool.core.collection.CollUtil;
import com.wudl.govern.db.DataSourceFactory;
import com.wudl.govern.db.DbQuery;
import com.wudl.govern.db.constants.DbQueryProperty;
import com.wudl.govern.db.core.DbColumn;
import com.wudl.govern.db.core.DbTable;
import com.wudl.govern.domain.DbSource;
import com.wudl.govern.domain.MetaColumn;
import com.wudl.govern.domain.MetaTable;
import com.wudl.govern.service.MetaColumnService;
import com.wudl.govern.service.MetaTableService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ：wudl
 * @date ：Created in 2022-04-03 17:03
 * @description：异步处理元数据
 * @modified By：
 * @version: 1.0
 */
@Slf4j
@Component
public class AsyncTask {
    @Autowired
    private DataSourceFactory dataSourceFactory;

    @Autowired
    MetaTableService metaTableService;

    @Autowired
    MetaColumnService  metaColumnService;


    @Async("taskExecutor")
    public void doTask(DbSource dbSource) {
        long start = System.currentTimeMillis();
        DbQueryProperty dbQueryProperty = new DbQueryProperty(dbSource.getDbType(), dbSource.getDbHost(),
                dbSource.getUserName(), dbSource.getPassWord(), dbSource.getDbPort(), dbSource.getDbName());
        DbQuery dbQuery = dataSourceFactory.createDbQuery(dbQueryProperty);
        List<DbTable> tables = dbQuery.getTables(dbSource.getDbName());
        if (CollUtil.isNotEmpty(tables)) {
            List<MetaTable> metadataTableEntityList = tables.stream().map(table -> {
                MetaTable metadataTable = new MetaTable();
                metadataTable.setDataSourceId(1);
                metadataTable.setTableName(table.getTableName());
                metadataTable.setRemarks(table.getTableComment());
                return metadataTable;
            }).collect(Collectors.toList());
            if (CollUtil.isNotEmpty(metadataTableEntityList)) {
                metadataTableEntityList.stream().forEach(table -> {
                    metaTableService.save(table);






                    List<DbColumn> columns = dbQuery.getTableColumns(dbSource.getDbName(), table.getTableName());
                    if (CollUtil.isNotEmpty(columns)) {
                        List<MetaColumn> metadataColumnEntityList = columns.stream().map(column -> {
                            MetaColumn metadataColumn = new MetaColumn();
                            metadataColumn.setColumnId(table.getId());
                            metadataColumn.setMetaTableId(table.getId());
                            metadataColumn.setColumnName(column.getColName());
                            metadataColumn.setRemarks(column.getColComment());
                            metadataColumn.setPartOfPrimaryKey(column.getColKey() ? 1 : 0);
                            metadataColumn.setNullable(column.getNullable() ? "1" : "0");
                            metadataColumn.setOrdinalPosition(column.getColPosition());
                            metadataColumn.setType(column.getDataType());
//                            metadataColumn.set(column.getDataLength());
//                            metadataColumn.setDataPrecision(column.getDataPrecision());
//                            metadataColumn.setScale(column.getDataScale());
                            metadataColumn.setDefaultValue(column.getDataDefault());
                            return metadataColumn;
                        }).collect(Collectors.toList());

                        if (CollUtil.isNotEmpty(metadataColumnEntityList)) {
                            metadataColumnEntityList.stream().forEach(column -> metaColumnService.save(column));
                        }
                    }
                });
            }
        }
//        dataSource.setIsSync(SyncStatus.IsSync.getKey());
//        metadataSourceDao.updateById(dataSource);
        log.info("异步任务执行完成！耗时{}秒", (System.currentTimeMillis() - start  / 1000));
    }

}
